/* eslint-disable prefer-const */
import { map } from 'lodash'

export const urlToList = (url: string): Array<string> => {
  if (url) {
    const urlList = url.split('/').filter((i) => i)
    return map(urlList, (urlItem, index) => `/${urlList.slice(0, index + 1).join('/')}`)
  }
  return []
}

export const urlToJson = (url = window.location.href) => {
  // 箭头函数默认传值为当前页面url
  let obj = {},
    index = url.indexOf('?'), // 看url有没有参数
    params = url.substring(index + 1) // 截取url参数部分 name = aaa & age = 20
  if (index != -1) {
    // 有参数时
    let parr = params.split('&') // 将参数分割成数组 ["name = aaa", "age = 20"]
    for (let i of parr) {
      // 遍历数组
      let arr = i.split('=') // 1） i name = aaa   arr = [name, aaa]  2）i age = 20  arr = [age, 20]
      obj[arr[0]] = arr[1] // obj[arr[0]] = name, obj.name = aaa   obj[arr[0]] = age, obj.age = 20
    }
  }
  return obj
}